/**
  ************************************* 版权***********************************
  *
  *                 (C) 版权所有 2024,柴志强 所有权利
  *
  *
  * 文件名     :   protocol.c
  * 版  本     :    v1.0
  * 作  者     :   柴志强
  * 日  期     :   2024-03-11
  * 功  能     :
  *
  ******************************************************************************
 */
#include "mf_config.h"
#include "rs485.h"
#include "rtc.h"
#include "uart.h"
#include "string.h"
#include "protocol.h"
#include "flash.h"

char dataArray[Data_ARRAY_MAX];
uint16_t dataArrayLeng=0;

char dataTemp[512];

float baseData[4];

const uint8_t modbusCMD1[8] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
const uint8_t modbusCMD2[8] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x02, 0x65, 0xCB};
const uint8_t modbusCMD3[8] = {0x01, 0x03, 0x00, 0x04, 0x00, 0x02, 0x85, 0xCA};
const uint8_t modbusCMD4[8] = {0x01, 0x03, 0x00, 0x06, 0x00, 0x02, 0x24, 0x0A};

uint8_t value[4];
void Protocol_Read_Data(void) {
    /**初始化一些发送数据**/
    memcpy(&dataTemp[0],"ABCD",4);//起始码
    memcpy(&dataTemp[17],&_DEVICE_INFOR_BYTE[4],15);//IMEI
    memcpy(&dataTemp[32],";1;01",5);//硬件版本1，软件版本01
    memcpy(&dataTemp[40],&_DEVICE_INFOR_BYTE[20],2);


    FL_RTC_GetTime(RTC, &GetTime);
    uartPrint(UART0, "\r\n[20%02x/%02x/%02x,%02x:%02x:%02x] ", GetTime.year, GetTime.month, GetTime.day, GetTime.hour, GetTime.minute, GetTime.second);
    DebugUrtPrint("Read RS485 Data\r\n{");
    if(RS485ReadData((unsigned char *)modbusCMD1, 8) == 1) {
        if(Rs485UartBuff[2] == 0x04) {
            memset(value, 0, 4);
            value[0] = Rs485UartBuff[6];
            value[1] = Rs485UartBuff[5];
            value[2] = Rs485UartBuff[4];
            value[3] = Rs485UartBuff[3];
            memcpy(&baseData[0], &value, 4);
            DebugUrtPrint("\r\n  ZLJ:%f \r\n", baseData[0]);
        }
    }

    if(RS485ReadData((unsigned char *)modbusCMD2, 8) == 1) {
        if(Rs485UartBuff[2] == 0x04) {
            memset(value, 0, 4);
            value[0] = Rs485UartBuff[6];
            value[1] = Rs485UartBuff[5];
            value[2] = Rs485UartBuff[4];
            value[3] = Rs485UartBuff[3];
            memcpy(&baseData[1], &value, 4);
            DebugUrtPrint("  FLJ:%f \r\n", baseData[1]);
        }
    }

    if(RS485ReadData((unsigned char *)modbusCMD3, 8) == 1) {
        if(Rs485UartBuff[2] == 0x04) {
            memset(value, 0, 4);
            value[0] = Rs485UartBuff[6];
            value[1] = Rs485UartBuff[5];
            value[2] = Rs485UartBuff[4];
            value[3] = Rs485UartBuff[3];
            memcpy(&baseData[2], &value, 4);
            DebugUrtPrint("  JLJ:%f \r\n", baseData[2]);
        }
    }

    if(RS485ReadData((unsigned char *)modbusCMD4, 8) == 1) {
        if(Rs485UartBuff[2] == 0x04) {
            memset(value, 0, 4);
            value[0] = Rs485UartBuff[6];
            value[1] = Rs485UartBuff[5];
            value[2] = Rs485UartBuff[4];
            value[3] = Rs485UartBuff[3];
            memcpy(&baseData[3], &value, 4);
            DebugUrtPrint("  SSL:%f \r\n}\r\n", baseData[3]);
        }
    }
}

void mergeData(void) {
    _DEVICE_INFOR_BYTE[0]='A';
    _DEVICE_INFOR_BYTE[1]='B';
    _DEVICE_INFOR_BYTE[1]='C';
    _DEVICE_INFOR_BYTE[1]='D';
}



